package com.juyu.miliao.api.util;

public class CallTimeUtil {
	/**
	 * 日时分秒转换为秒数
	 * 
	 * @param
	 * @return
	 */
	public static Long timeToSecond(String str) {
		String[] sarray = str.split(":");
		Long temp = 0L;
		if (sarray.length == 4) {
			Long a1 = (long) ((Long.parseLong(sarray[1])) * (Math.pow(60, 2)));
			Long a2 = (long) ((Long.parseLong(sarray[2])) * (Math.pow(60, 1)));
			Long a3 = (long) ((Long.parseLong(sarray[3])) * (Math.pow(60, 0)));
			temp = a1 + a2 + a3;
		} else {
			for (int i = 0; i < sarray.length; i++) {
				int k = sarray.length - i - 1;
				temp += (long) ((long) ((Long.parseLong(sarray[i])) * (Math.pow(60, k))));
			}
		}
		return temp;
	}

	/**
	 * 将秒数转换为日时分秒
	 * 
	 * @param second
	 * @return
	 */
	public static String secondToTime(long second) {
		long days = second / 86400; // 转换天数
		second = second % 86400; // 剩余秒数
		long hours = second / 3600; // 转换小时
		hours += days * 24;
		String strhours = hours + "";
		if (hours < 10)
			strhours = "0" + hours;
		second = second % 3600; // 剩余秒数
		long minutes = second / 60; // 转换分钟
		String strminutes = minutes + "";
		if (minutes < 10)
			strminutes = "0" + minutes;
		second = second % 60; // 剩余秒数
		String strsecond = second + "";
		if (second < 10)
			strsecond = "0" + second;
		return strhours + ":" + strminutes + ":" + strsecond;
	}
	/**
	 * 将秒数转换为日时分秒
	 *
	 * @param second
	 * @return
	 */
	public static String secondToFormTime(long second) {
		long days = second / 86400; // 转换天数
		second = second % 86400; // 剩余秒数
		long hours = second / 3600; // 转换小时
		hours += days * 24;
		String strhours = hours + "";
		if (hours < 10)
			strhours = "0" + hours;
		second = second % 3600; // 剩余秒数
		long minutes = second / 60; // 转换分钟
		String strminutes = minutes + "";
		if (minutes < 10)
			strminutes = "0" + minutes;
		second = second % 60; // 剩余秒数
		String strsecond = second + "";
		if (second < 10)
			strsecond = "0" + second;
		return strhours + "小时" + strminutes + "分" + strsecond+"秒";
	}

	/**
	 * 毫秒转化--x时x分x秒x毫秒
	 * @param ms
	 * @return
	 */
	public static String formatTime(Long ms) {
		Integer ss = 1000;
		Integer mi = ss * 60;
		Integer hh = mi * 60;
		Integer dd = hh * 24;

		Long day = ms / dd;
		Long hour = (ms - day * dd) / hh;
		Long minute = (ms - day * dd - hour * hh) / mi;
		Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
		Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

		StringBuffer sb = new StringBuffer();
		if (day > 0) {
			sb.append(day + "天");
		}
		if (hour > 0) {
			sb.append(hour + "小时");
		}
		if (minute > 0) {
			sb.append(minute + "分");
		}
		if (second > 0) {
			sb.append(second + "秒");
		}
		if (milliSecond > 0) {
			sb.append(milliSecond + "毫秒");
		}
		return sb.toString();
	}
}
